{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 98,
   "metadata": {},
   "outputs": [],
   "source": [
    "#-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=\n",
    "# GSEPacket Class and Class Functions\n",
    "#-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=\n",
    "import struct"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#GSE_length\n",
    "#This 12-bit field indicates the length, in bytes, of the GSE Packet \n",
    "#counted from the byte following this GSE Length field. The GSE Length \n",
    "#field allows for a length of up to 4096 bytes for a GSE Packet. \n",
    "#The GSE Length field points to the start of the following GSE Packet, \n",
    "#or to the end of the Data Field or start of the padding field if the \n",
    "#GSE packet is the last in the frame."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 99,
   "metadata": {},
   "outputs": [],
   "source": [
    "class GSEPacket:\n",
    "    def __init__(self,\n",
    "                 num_BBF, \n",
    "                 start_indicator, \n",
    "                 stop_indicator, \n",
    "                 label_type, \n",
    "                 GSE_length, \n",
    "                 fragment_ID, \n",
    "                 total_length,\n",
    "                 H_LEN, \n",
    "                 H_TYPE,\n",
    "                 zero_prefix = 0b00000):\n",
    "        self.num_BBF = num_BBF\n",
    "        self.start_indicator = start_indicator\n",
    "        self.stop_indicator = stop_indicator\n",
    "        self.label_type = label_type\n",
    "        self.GSE_length = GSE_length\n",
    "        self.fragment_ID = fragment_ID\n",
    "        self.total_length = total_length\n",
    "        self.H_LEN = H_LEN\n",
    "        self.H_TYPE = H_TYPE\n",
    "        self.zero_prefix = zero_prefix\n",
    "        \n",
    "    def build_header(self):\n",
    "        packet = struct.pack(\n",
    "            '!Hcc',\n",
    "            self.num_BBF,\n",
    "            self.start_indicator,\n",
    "            self.stop_indicator,\n",
    "        )\n",
    "        return packet\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 100,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "b'\\x00@sd'\n",
      "(64, b's', b'd')\n"
     ]
    }
   ],
   "source": [
    "\n",
    "#just to make sure we understand struct   \n",
    "myGSE = GSEPacket(64, b's', b'd', 3, 4, 5, 6, 7, 8)\n",
    "GSEH = myGSE.build_header()\n",
    "\n",
    "print(GSEH)\n",
    "print(struct.unpack('!Hcc', GSEH ))\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
